package com.poisson;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Random;

public class PoissonMain {

    private static final double t0 = 0;

    public static void main(String[] args) {
        // 添加面板
        PoissonFrame.getFrame().add(InputPanel.getPanel());

        /*
         * 调用用户定义的方法并添加组件到面板
         */
        placeInput(InputPanel.getPanel());


        PoissonFrame.getFrame().setLocationRelativeTo(null);

        // 设置界面可见
        PoissonFrame.getFrame().setVisible(true);

    }

    private static void placeInput(JPanel panel) {

        /* 布局部分我们这边不多做介绍
         * 这边设置布局为 null
         */
        panel.setLayout(null);

        /**
         * 参数lamda
         */
        JLabel lamdaLabel = new JLabel("参数lamda:");
        lamdaLabel.setBounds(10, 45, 200, 25);
        panel.add(lamdaLabel);

        /**
         * 生成个数
         */
        JTextField lamdaText = new JTextField();
        lamdaText.setBounds(10, 80, 50, 25);
        panel.add(lamdaText);

        /**
         * 生成个数
         */
        JLabel numberCountLabel = new JLabel("生成个数:");
        numberCountLabel.setBounds(10, 130, 200, 25);
        panel.add(numberCountLabel);

        /**
         * 生成个数
         */
        JTextField randomNumberCountText = new JTextField();
        randomNumberCountText.setBounds(10, 170, 70, 25);
        panel.add(randomNumberCountText);

        /**
         * 是否批量
         */
        JLabel isBatchLabel = new JLabel("是否批量:");
        isBatchLabel.setBounds(10, 220, 200, 25);
        panel.add(isBatchLabel);

        /**
         * 是否批量
         */
        JComboBox isBatchBox = new JComboBox<>();
        isBatchBox.setBounds(10, 260, 130, 25);
        isBatchBox.addItem("0-不批量");
        isBatchBox.addItem("1-批量");
        panel.add(isBatchBox);


        /**
         * 请输入生成Bi的最大值（从0开始）
         */
        JLabel maxBiLabel = new JLabel("请输入生成Bi的最大值（从0开始）:");
        maxBiLabel.setBounds(250, 220, 400, 25);
        panel.add(maxBiLabel);

        /**
         * 请输入生成Bi的最大值（从0开始）
         */
        JTextField maxBiText = new JTextField();
        maxBiText.setBounds(250, 260, 50, 25);
        panel.add(maxBiText);

        /**
         * 确定展示按钮
         */
        JButton showButton = new JButton("确定");
        showButton.setBounds(10, 450, 80, 25);
        panel.add(showButton);
        showButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Double[] numberArray = generateRandomNumbers(Double.valueOf(lamdaText.getText().trim()), Integer.valueOf(randomNumberCountText.getText().trim()));
                boolean isBatch = isBatchBox.getSelectedItem().toString().charAt(0) == '1';
                showResult(numberArray, isBatch, isBatch ? Integer.valueOf(maxBiText.getText().trim()) : null);
                System.err.println("exit");
            }
        });
    }

    private static Double[] generateRandomNumbers(double lamda, int randomNumberCount) {
        Random random = new Random();
        double u = random.nextDouble();
        System.err.printf("generateRandomNumbers lamda=%s,u=%s,randomNumberCount=%d \n", lamda, u, randomNumberCount);
        Double[] numberArray = new Double[randomNumberCount];
        numberArray[0] = t0;
        for (int i = 1; i < numberArray.length; i++) {
            numberArray[i] = numberArray[i - 1] - (1 / lamda) * (Math.log(u) / Math.log(Math.E));
        }
        System.err.println(Arrays.toString(numberArray));
        return numberArray;
    }

    private static void showResult(Double[] numberArray, boolean isBatch, Integer maxBi) {
        System.err.printf("showResult numberArray=%s,isBatch=%s,maxBi=%s \n", Arrays.toString(numberArray), isBatch, maxBi);
        if (isBatch) {
            Double[] biNumberArray = new Double[numberArray.length];

            for (int i = 0; i < numberArray.length; i++) {
                Random random = new Random();
                biNumberArray[i] = Double.valueOf(random.nextInt(maxBi)) ;
            }
            PaintTableService.paint(numberArray, biNumberArray);
            System.err.printf("showResult bNumberArray=%s \n", Arrays.toString(biNumberArray));
            PaintCoordinateService.paintCoordinate(numberArray, biNumberArray);
        } else {
            PaintTableService.paint(numberArray);
            Double[] xNumberArray = new Double[numberArray.length];
            for (int i = 0; i < numberArray.length; i++) {
                xNumberArray[i] = Double.valueOf(i);
            }
            PaintCoordinateService.paintCoordinate(xNumberArray, numberArray);
        }
    }

}